package com.wlhse.service.impl;

import com.wlhse.dao.TaskAddDao;
import com.wlhse.dto.getDto.*;
import com.wlhse.dto.inDto.SuperVisionTaskPersonInDto;
import com.wlhse.dto.inDto.SupervisionTaskDetailInDto;
import com.wlhse.entity.*;
import com.wlhse.service.TaskAddService;
import com.wlhse.util.DateUtil;
import com.wlhse.util.GetCurrentUserIdUtil;
import com.wlhse.util.TreeUtil;
import com.wlhse.util.state_code.NR;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskAddServiceImpl implements TaskAddService {
    @Resource
    private TaskAddDao taskAddDao;

    TreeUtil treeUtil=new TreeUtil();
    DateUtil dateUtil=new DateUtil();

    @Resource
    private GetCurrentUserIdUtil getCurrentUserIdUtil;

    @Override
    public String addTask(AddTaskGetDto addTaskGetDto, HttpServletRequest request) {
        String verify_startDate=addTaskGetDto.getStartDate();
        String verify_endDate=addTaskGetDto.getEndDate();
        String time=verify_startDate.replace("-","")+verify_endDate.replace("-","");
        //将当前时间作为唯一任务CODE
        addTaskGetDto.setSupervisionTaskCode("G01-" + time);

//        获取当前登陆人ID和姓名
        Integer userid = getCurrentUserIdUtil.getUserId(request);
        String uname = taskAddDao.queryEmployeeName(userid).getName();

        //生成五个List，避免空指针异常
        List<TaskJsonDto> listTeam = new ArrayList<TaskJsonDto>();
        listTeam = addTaskGetDto.getTableTeam();
        List<TaskJsonDto> listGroup = new ArrayList<TaskJsonDto>();
        listGroup = addTaskGetDto.getTableGroup();
        List<TaskJsonDto> listLocation = new ArrayList<TaskJsonDto>();
        listLocation = addTaskGetDto.getTableLocation();
        List<TaskKeyAndCheckDto> listKey = new ArrayList<TaskKeyAndCheckDto>();
        listKey = addTaskGetDto.getTableKey();
        List<TaskKeyAndCheckDto> listCheck = new ArrayList<TaskKeyAndCheckDto>();
        listCheck = addTaskGetDto.getTableCheck();
        StaffGetDto listStaff = addTaskGetDto.getTableStaff();

        //验证任务时间及轮次是否重合（轮次暂时取消）
        boolean time_flag=false;
        Integer db_round=taskAddDao.queryCurrentRound();
//        boolean round_flag= roundUtil.roundIstrue(addTaskGetDto.getRound(),db_round);

        //根据人名来判断时间是否重复
        String falseName=null;
        String db_startDate=null;
        String db_endDate=null;
        for(String staffname:listStaff.getStaffName()){
            db_startDate=taskAddDao.queryStartDateByName(staffname);
            db_endDate=taskAddDao.queryEndDateByName(staffname);
            //判断数据库中是否有时间，如果没有，则time_flag为true
            if(db_endDate==null||db_startDate==null){
                time_flag=true;
            }else{
                time_flag=dateUtil.isEffectiveDate(verify_startDate,verify_endDate,db_startDate,db_endDate);
            }
            System.out.println(time_flag);
            if(!time_flag){
                falseName=staffname;
                break;
            }
        }

        if(!time_flag){
            return NR.getDateAndRound(time_flag,true,falseName,db_round,db_startDate,db_endDate);
        }else {
            //生成该任务表，每个人都只有各自的一个记录（SupervisionTask）
            for(String staffname:listStaff.getStaffName()){
                SupervisionTaskPojo2 taskPojo = new SupervisionTaskPojo2();
                Integer temp_id = taskAddDao.queryStaffIdByName(staffname);
                taskPojo.setSupervisionTaskCode(addTaskGetDto.getSupervisionTaskCode() + "-" + temp_id);
                taskPojo.setStartDate(addTaskGetDto.getStartDate());
                taskPojo.setEndDate(addTaskGetDto.getEndDate());
                taskPojo.setIssueStaffID(userid);
                taskPojo.setIssueStaffName(uname);
                taskPojo.setExecStaffID(temp_id);
                taskPojo.setExecStaffName(staffname);
                taskPojo.setStatus("未接收");
                taskPojo.setExecstatus("未完成");
                taskPojo.setRound(addTaskGetDto.getRound());
                taskAddDao.addTask(taskPojo);
            }

            //生成supervisionstaskdetial2
            for (TaskJsonDto team : listTeam) {
                if (!team.getPlannedInspectionTimes().equals("0")) {
                    SupervisionTaskDetailInDto detailPojo2 = new SupervisionTaskDetailInDto();
                    detailPojo2.setSupervisionTaskCode(addTaskGetDto.getSupervisionTaskCode() + "-" + team.getStaffID());
                    detailPojo2.setStaffID(team.getStaffID());
                    detailPojo2.setStaffName(team.getStaffName());
                    detailPojo2.setTaskType(team.getTaskType());
                    detailPojo2.setTaskItem(team.getTaskItem());
                    detailPojo2.setCompanyCode(team.getCompanyCode());
                    detailPojo2.setPlannedInspectionTimes(team.getPlannedInspectionTimes());
                    detailPojo2.setActuaInspectionTimes("0");
                    detailPojo2.setTaskDetailType("规定");
                    detailPojo2.setAreaName(team.getAreaName());
                    taskAddDao.addTaskDetail(detailPojo2);
                }
            }
            for (TaskJsonDto group : listGroup) {
                if (!group.getPlannedInspectionTimes().equals("0")) {
                    SupervisionTaskDetailInDto detailPojo2 = new SupervisionTaskDetailInDto();
                    detailPojo2.setSupervisionTaskCode(addTaskGetDto.getSupervisionTaskCode() + "-" + group.getStaffID());
                    detailPojo2.setStaffID(group.getStaffID());
                    detailPojo2.setStaffName(group.getStaffName());
                    detailPojo2.setTaskType(group.getTaskType());
                    detailPojo2.setTaskItem(group.getTaskItem());
                    detailPojo2.setCompanyCode(group.getCompanyCode());
                    detailPojo2.setPlannedInspectionTimes(group.getPlannedInspectionTimes());
                    detailPojo2.setActuaInspectionTimes("0");
                    detailPojo2.setTaskDetailType("规定");
                    detailPojo2.setAreaName(group.getAreaName());
                    taskAddDao.addTaskDetail(detailPojo2);
                }
            }
            for (TaskJsonDto location : listLocation) {
                if (!location.getPlannedInspectionTimes().equals("0")) {
                    SupervisionTaskDetailInDto detailPojo2 = new SupervisionTaskDetailInDto();
                    detailPojo2.setSupervisionTaskCode(addTaskGetDto.getSupervisionTaskCode() + "-" + location.getStaffID());
                    detailPojo2.setStaffID(location.getStaffID());
                    detailPojo2.setStaffName(location.getStaffName());
                    detailPojo2.setTaskType(location.getTaskType());
                    detailPojo2.setTaskItem(location.getTaskItem());
                    detailPojo2.setCompanyCode(location.getCompanyCode());
                    detailPojo2.setPlannedInspectionTimes(location.getPlannedInspectionTimes());
                    detailPojo2.setActuaInspectionTimes("0");
                    detailPojo2.setTaskDetailType("规定");
                    detailPojo2.setAreaName(location.getAreaName());
                    taskAddDao.addTaskDetail(detailPojo2);
                }
            }

            for (TaskKeyAndCheckDto key : listKey) {
                if (!key.getPlannedInspectionTimes().equals("0")) {
                    SupervisionTaskDetailInDto detailPojo2 = new SupervisionTaskDetailInDto();
                    detailPojo2.setSupervisionTaskCode(addTaskGetDto.getSupervisionTaskCode() + "-" + key.getStaffID());
                    detailPojo2.setStaffID(key.getStaffID());
                    detailPojo2.setStaffName(key.getStaffName());
                    detailPojo2.setTaskType(key.getTaskType());
                    detailPojo2.setTaskItem(key.getTaskItem());
                    detailPojo2.setCompanyCode(key.getCompanyCode());
                    detailPojo2.setPlannedInspectionTimes(key.getPlannedInspectionTimes());
                    detailPojo2.setActuaInspectionTimes("0");
                    detailPojo2.setTaskDetailType("规定");
                    detailPojo2.setAreaName(key.getAreaName());
                    taskAddDao.addTaskDetail(detailPojo2);
                }
            }

            for (TaskKeyAndCheckDto check : listCheck) {
                if (!check.getPlannedInspectionTimes().equals("0")) {
                    SupervisionTaskDetailInDto detailPojo2 = new SupervisionTaskDetailInDto();
                    detailPojo2.setSupervisionTaskCode(addTaskGetDto.getSupervisionTaskCode() + "-" + check.getStaffID());
                    detailPojo2.setStaffID(check.getStaffID());
                    detailPojo2.setStaffName(check.getStaffName());
                    detailPojo2.setTaskType(check.getTaskType());
                    detailPojo2.setTaskItem(check.getTaskItem());
                    detailPojo2.setCompanyCode(check.getCompanyCode());
                    detailPojo2.setPlannedInspectionTimes(check.getPlannedInspectionTimes());
                    detailPojo2.setActuaInspectionTimes("0");
                    detailPojo2.setTaskDetailType("规定");
                    detailPojo2.setAreaName(check.getAreaName());
                    taskAddDao.addTaskDetail(detailPojo2);
                }
            }

            //生成supervisiontaskperson
            for(String taskperson:listStaff.getStaffName()){
                SuperVisionTaskPersonInDto superVisionTaskPersonInDto = new SuperVisionTaskPersonInDto();
                Integer exec_id=taskAddDao.queryStaffIdByName(taskperson);
                String querycode=addTaskGetDto.getSupervisionTaskCode()+"-"+exec_id;
                superVisionTaskPersonInDto.setSupervisionTaskID(taskAddDao.querytaskIdByCode(querycode));
                superVisionTaskPersonInDto.setStartDate(addTaskGetDto.getStartDate());
                superVisionTaskPersonInDto.setEndDate(addTaskGetDto.getEndDate());
                superVisionTaskPersonInDto.setStaffID(exec_id);
                taskAddDao.addTaskPerson(superVisionTaskPersonInDto);
            }
            return NR.r();
        }
    }

    @Override
    public String getUser(HttpServletRequest request) {
        Integer userid=getCurrentUserIdUtil.getUserId(request);
        String name=taskAddDao.queryEmployeeName(userid).getName();
        return NR.r(name);
    }

    @Override
    public String getArea() {
        List<DataDictPojo> list=taskAddDao.getArea();
        return NR.r(list);
    }

    @Override
    public String getPersonByArea(String dictcode) {
        List<SuperVisionScopePojo>  list = null;
        list = taskAddDao.getPerson(dictcode);
        return NR.r(list);
    }

    @Override
    public String getTaskByName(StaffIDGetDto staffIDList) {
        List<SuperVisionScopePojo> team=new ArrayList<SuperVisionScopePojo>();
        List<SuperVisionScopePojo> group=new ArrayList<SuperVisionScopePojo>();
        List<SuperVisionScopePojo> location=new ArrayList<SuperVisionScopePojo>();
        List<SuperVisionScopePojo2> key=new ArrayList<>();
        List<SuperVisionScopePojo2> check=new ArrayList<>();
        for(String staffname:staffIDList.getStaffName()){
            List<SuperVisionScopePojo> list_team = taskAddDao.getteam(staffname,"作业队");
            List<SuperVisionScopePojo> list_group = taskAddDao.getgroup(staffname,"配属班组");
            List<SuperVisionScopePojo> list_location = taskAddDao.getlocation(staffname,"重点要害部位及固定场所");
            List<SuperVisionScopePojo2> list_key= taskAddDao.getkey(staffname,"专项检查");
            List<SuperVisionScopePojo2> list_check = taskAddDao.getcheck(staffname,"关键环节");
            for(SuperVisionScopePojo superVisionScopePojo:list_team){
                team.add(superVisionScopePojo);
            }
            for(SuperVisionScopePojo superVisionScopePojo:list_group){
                group.add(superVisionScopePojo);
            }
            for(SuperVisionScopePojo superVisionScopePojo:list_location){
                location.add(superVisionScopePojo);
            }
            for(SuperVisionScopePojo2 superVisionScopePojo2:list_key){
                key.add(superVisionScopePojo2);
            }
            for(SuperVisionScopePojo2 superVisionScopePojo2:list_check){
                check.add(superVisionScopePojo2);
            }
        }
        return NR.task(team,group,location,key,check);
    }

    @Override
    public String queryTaskDetailByCode(String taskCode) {
        List<SupervisionTaskDetailInDto> team = taskAddDao.queryCurrentTaskDetail(taskCode,"作业队");
        List<SupervisionTaskDetailInDto> group = taskAddDao.queryCurrentTaskDetail(taskCode,"配属班组");
        List<SupervisionTaskDetailInDto> location = taskAddDao.queryCurrentTaskDetail(taskCode,"重点要害部位及固定场所");
        List<SupervisionTaskDetailInDto> key = taskAddDao.queryCurrentTaskDetail(taskCode,"专项检查");
        List<SupervisionTaskDetailInDto> check = taskAddDao.queryCurrentTaskDetail(taskCode,"关键环节");
        SupervisionTaskPojo supervisionTaskPojo = taskAddDao.queryCurrentTaskDate(taskCode);
        return NR.taskdetail(team,group,location,key,check,supervisionTaskPojo);
    }

    @Override
    public String manageTask(String taskCode,String type,String areaName) {
        //目前已有的任务
        List<SupervisionTaskDetailInDto> current_task=taskAddDao.queryCurrentTaskDetail(taskCode,type);
        //数据库中的任务
        if(type.equals("专项检查")||type.equals("关键环节")){
            List<SuperVisionScopePojo2> db_task=new ArrayList<>();
            db_task=taskAddDao.queryTaskScope2(type);
            for(int i = current_task.size() - 1; i >= 0; i--) {
                for (int j = db_task.size() - 1; j >= 0; j--) {
                    if(current_task.get(i).getTaskItem().equals(db_task.get(j).getTaskItem())){
                        db_task.remove(db_task.get(j));
                    }
                }
            }
            return NR.r(db_task);
        }else{
            List<SuperVisionAreaCompany> db_task1=new ArrayList<>();
            db_task1=taskAddDao.queryTaskScope(areaName,type);
            for(int i = current_task.size() - 1; i >= 0; i--) {
                for (int j = db_task1.size() - 1; j >= 0; j--) {
                    if(current_task.get(i).getTaskItem().equals(db_task1.get(j).getTaskItem())){
                        db_task1.remove(db_task1.get(j));
                    }
                }
            }
            return NR.r(db_task1);
        }
    }

    @Override
    @Transactional
    public String addNewTask(AddTaskGetDto addTaskGetDto) {
        //生成五个List，避免空指针异常
        List<TaskJsonDto> listTeam = new ArrayList<TaskJsonDto>();
        listTeam = addTaskGetDto.getTableTeam();
        List<TaskJsonDto> listGroup = new ArrayList<TaskJsonDto>();
        listGroup = addTaskGetDto.getTableGroup();
        List<TaskJsonDto> listLocation = new ArrayList<TaskJsonDto>();
        listLocation = addTaskGetDto.getTableLocation();
        List<TaskKeyAndCheckDto> listKey = new ArrayList<TaskKeyAndCheckDto>();
        listKey = addTaskGetDto.getTableKey();
        List<TaskKeyAndCheckDto> listCheck = new ArrayList<TaskKeyAndCheckDto>();
        listCheck = addTaskGetDto.getTableCheck();
        StaffGetDto listStaff = addTaskGetDto.getTableStaff();

        //删除该任务下所有的supervisionstaskdetial2
        taskAddDao.deleteByCode(addTaskGetDto.getSupervisionTaskCode());

        //生成supervisionstaskdetial2
        //tip为1代表是修改任务界面传过来的
        String task_detail_type=null;
        if(addTaskGetDto.getTip().equals(1)){
            task_detail_type="规定";
        }else {
            task_detail_type="自选";
        }
        for (TaskJsonDto team : listTeam) {
            if (!team.getPlannedInspectionTimes().equals("0")) {
                SupervisionTaskDetailInDto detailPojo2 = new SupervisionTaskDetailInDto();
                detailPojo2.setSupervisionTaskCode(addTaskGetDto.getSupervisionTaskCode());
                detailPojo2.setStaffID(team.getStaffID());
                detailPojo2.setStaffName(team.getStaffName());
                detailPojo2.setTaskType(team.getTaskType());
                detailPojo2.setTaskItem(team.getTaskItem());
                detailPojo2.setCompanyCode(team.getCompanyCode());
                detailPojo2.setPlannedInspectionTimes(team.getPlannedInspectionTimes());
                //新增为0，修改任务传的时候tip为1，actuaInspectionTimes为数据库中的次数,新增为0
                if(addTaskGetDto.getTip().equals(0)){
                    team.setActuaInspectionTimes("0");
                }
                detailPojo2.setActuaInspectionTimes(team.getActuaInspectionTimes());
                detailPojo2.setTaskDetailType(task_detail_type);
                detailPojo2.setAreaName(team.getAreaName());
                taskAddDao.addTaskDetail(detailPojo2);
            }
        }
        for (TaskJsonDto group : listGroup) {
            if (!group.getPlannedInspectionTimes().equals("0")) {
                SupervisionTaskDetailInDto detailPojo2 = new SupervisionTaskDetailInDto();
                detailPojo2.setSupervisionTaskCode(addTaskGetDto.getSupervisionTaskCode() );
                detailPojo2.setStaffID(group.getStaffID());
                detailPojo2.setStaffName(group.getStaffName());
                detailPojo2.setTaskType(group.getTaskType());
                detailPojo2.setTaskItem(group.getTaskItem());
                detailPojo2.setCompanyCode(group.getCompanyCode());
                detailPojo2.setPlannedInspectionTimes(group.getPlannedInspectionTimes());
                //新增为0，修改任务传的时候tip为1，actuaInspectionTimes为数据库中的次数,新增为0
                if(addTaskGetDto.getTip().equals(0)){
                    group.setActuaInspectionTimes("0");
                }
                detailPojo2.setActuaInspectionTimes(group.getActuaInspectionTimes());
                detailPojo2.setTaskDetailType(task_detail_type);
                detailPojo2.setAreaName(group.getAreaName());
                taskAddDao.addTaskDetail(detailPojo2);
            }
        }
        for (TaskJsonDto location : listLocation) {
            if (!location.getPlannedInspectionTimes().equals("0")) {
                SupervisionTaskDetailInDto detailPojo2 = new SupervisionTaskDetailInDto();
                detailPojo2.setSupervisionTaskCode(addTaskGetDto.getSupervisionTaskCode());
                detailPojo2.setStaffID(location.getStaffID());
                detailPojo2.setStaffName(location.getStaffName());
                detailPojo2.setTaskType(location.getTaskType());
                detailPojo2.setTaskItem(location.getTaskItem());
                detailPojo2.setCompanyCode(location.getCompanyCode());
                detailPojo2.setPlannedInspectionTimes(location.getPlannedInspectionTimes());
                //新增为0，修改任务传的时候tip为1，actuaInspectionTimes为数据库中的次数,新增为0
                if(addTaskGetDto.getTip().equals(0)){
                    location.setActuaInspectionTimes("0");
                }
                detailPojo2.setActuaInspectionTimes(location.getActuaInspectionTimes());
                detailPojo2.setTaskDetailType(task_detail_type);
                detailPojo2.setAreaName(location.getAreaName());
                taskAddDao.addTaskDetail(detailPojo2);
            }
        }

        for (TaskKeyAndCheckDto key : listKey) {
            if (!key.getPlannedInspectionTimes().equals("0")) {
                SupervisionTaskDetailInDto detailPojo2 = new SupervisionTaskDetailInDto();
                detailPojo2.setSupervisionTaskCode(addTaskGetDto.getSupervisionTaskCode());
                detailPojo2.setStaffID(key.getStaffID());
                detailPojo2.setStaffName(key.getStaffName());
                detailPojo2.setTaskType(key.getTaskType());
                detailPojo2.setTaskItem(key.getTaskItem());
                detailPojo2.setCompanyCode(key.getCompanyCode());
                detailPojo2.setPlannedInspectionTimes(key.getPlannedInspectionTimes());
                //新增为0，修改任务传的时候tip为1，actuaInspectionTimes为数据库中的次数,新增为0
                if(addTaskGetDto.getTip().equals(0)){
                    key.setActuaInspectionTimes("0");
                }
                detailPojo2.setActuaInspectionTimes(key.getActuaInspectionTimes());
                detailPojo2.setTaskDetailType(task_detail_type);
                taskAddDao.addTaskDetail(detailPojo2);
            }
        }

        for (TaskKeyAndCheckDto check : listCheck) {
            if (!check.getPlannedInspectionTimes().equals("0")) {
                SupervisionTaskDetailInDto detailPojo2 = new SupervisionTaskDetailInDto();
                detailPojo2.setSupervisionTaskCode(addTaskGetDto.getSupervisionTaskCode());
                detailPojo2.setStaffID(check.getStaffID());
                detailPojo2.setStaffName(check.getStaffName());
                detailPojo2.setTaskType(check.getTaskType());
                detailPojo2.setTaskItem(check.getTaskItem());
                detailPojo2.setCompanyCode(check.getCompanyCode());
                detailPojo2.setPlannedInspectionTimes(check.getPlannedInspectionTimes());
                //新增为0，修改任务传的时候tip为1，actuaInspectionTimes为数据库中的次数,新增为0
                if(addTaskGetDto.getTip().equals(0)){
                    check.setActuaInspectionTimes("0");
                }
                detailPojo2.setActuaInspectionTimes(check.getActuaInspectionTimes());
                detailPojo2.setTaskDetailType(task_detail_type);
                taskAddDao.addTaskDetail(detailPojo2);
            }
        }

        //修改任务时间(两张表)
        if((addTaskGetDto.getStartDate()!=null && !"".equals(addTaskGetDto.getStartDate()))
                || (addTaskGetDto.getEndDate()!=null && !"".equals(addTaskGetDto.getEndDate())) ){
            taskAddDao.updateTaskDate(addTaskGetDto);
        }
        return NR.r();
    }


//    @Override
//    public String getTaskTypeDetail(String id) {
//        String typeid="%"+id+"%";
//        System.out.println(typeid);
//        List<DataDictPojo> list=taskAddDao.getTaskTypeDetail(typeid);
//        return NR.r(list);
//    }
}
